package com.beifu.manager.service.assetouttruthservice.Impl;

import com.beifu.manager.common.Const;
import com.beifu.manager.dao.*;
import com.beifu.manager.model.*;
import com.beifu.manager.service.assetoutplanservice.AssetOutPlanService;
import com.beifu.manager.service.assetouttruthservice.AssetOutTruthService;
import com.beifu.manager.vo.assetout.AssetOutDetailVo;
import com.mds.manager.utils.DateUtils;
import com.mds.manager.utils.PageUtils;
import com.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: licy
 * @Date :  2018/6/17 20:38
 * @Last Modified by: licy
 * @Last Modified time:  2018/6/17 20:38
 */
@Service
public class AssetOutTruthServiceImpl implements AssetOutTruthService {

    @Autowired
    private AssetOutTruthMapper assetOutTruthMapper;
    @Autowired
    private AssetInfoMapper assetInfoMapper;
    @Autowired
    private AssetBaseMapper assetBaseMapper;
    @Autowired
    private AssetTypeMapper assetTypeMapper;
    @Autowired
    private AssetOutPlanService assetOutPlanService;

    @Override
    public AssetOutDetailVo getOneTruthDetail(String assetOutId, boolean washFlag, int fin) {

        AssetOutDetailVo assetOutDetailVo = new AssetOutDetailVo();

//        List<AssetOutTruth> assetOutTruths = getAssetOutTruths(assetOutId);
//
//
//        HashMap<Integer, Integer> hashMap = getIntegerIntegerHashMap(assetOutTruths);
//
//        List<AssetOutPlan> tempList = new ArrayList<AssetOutPlan>();
//
//
//        for (Map.Entry<Integer, Integer> entry : hashMap.entrySet()) {
//            AssetOutPlan assetOutPlan = new AssetOutPlan();
//            Integer assetTypeId = entry.getKey();
//            Integer assetTruthCount = entry.getValue();
//            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
//            String assetTypeName = assetType.getName();
//            assetOutPlan.setAssetTypeId(assetTypeId);
//            assetOutPlan.setTruthCount(assetTruthCount);
//            assetOutPlan.setTypeName(assetTypeName);
//            tempList.add(assetOutPlan);
//        }

        List<AssetOutPlan> planList = assetOutPlanService.getAssetOutPlans(assetOutId,washFlag);
//        for(AssetOutPlan assetOutPlan:planList){
//            for(AssetOutPlan temp:tempList){
//                if(assetOutPlan.getAssetTypeId().equals(temp.getAssetTypeId())){
//                    assetOutPlan.setTruthCount(temp.getTruthCount());
//
//                    assetOutPlan.setTypeName(temp.getTypeName());
//                }
//            }
//        }

        for(AssetOutPlan assetOutPlan:planList){
            if(null==assetOutPlan.getTruthCount()){
                assetOutPlan.setTruthCount(0);
            }
        }

//        assetOutDetailVo.setAssetOutTruthsList(assetOutTruths);
        assetOutDetailVo.setAssetOutPlansList(planList);
        return assetOutDetailVo;
    }

    public List<AssetOutTruth> getAssetOutTruths(String assetOutId) {
        Long idalong = Long.valueOf(assetOutId);
        AssetOutTruthExample assetOutTruthExample  = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(idalong).andSecondScanEqualTo(Const.Second_Scan.YES);
        return assetOutTruthMapper.selectByExample(assetOutTruthExample);
//        return new ArrayList<AssetOutTruth>();
    }


    public HashMap<Integer, Integer> getIntegerIntegerHashMap(List<AssetOutTruth> assetOutTruths) {
        HashMap<Integer,Integer> hashMap = new HashMap<Integer, Integer>();
        for(AssetOutTruth assetOutTruth : assetOutTruths){
            String assetInfoRfidCode = assetOutTruth.getAssetInfoRfidCode();
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(assetInfoRfidCode);
            List<AssetInfo> assetInfos = assetInfoMapper.selectByExample(assetInfoExample);
            //TODO 如果没有应该有报错
            if(assetInfos!=null && assetInfos.size() > 0){
                AssetInfo assetInfo = assetInfos.get(0);
                Integer status = assetInfo.getStatus();
                Constants.ASSET_INFO_STATUS assetStatus = Constants.ASSET_INFO_STATUS.codeOf(String.valueOf(status));
                String assetStatusValue = assetStatus.getName();

                assetOutTruth.setStatusStr(assetStatusValue);

                assetOutTruth.setStatus(assetInfo.getStatus());
                Integer assetBaseid = assetInfo.getAssetBaseid();
                AssetBase assetBase = assetBaseMapper.selectByPrimaryKey(assetBaseid);
                Integer assetTypeId = assetBase.getAssetTypeId();
                AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
                String name = assetType.getName();
                assetOutTruth.setTypeName(name);
                if(hashMap.get(assetTypeId)==null){
                    hashMap.put(assetTypeId ,1 );
                }else {
                    int count = hashMap.get(assetTypeId);
                    count++;
                    hashMap.put(assetTypeId ,count );
                }
            }else {
                throw new RuntimeException("没有找到对应的RFID:"+assetInfoRfidCode);
            }
        }
        return hashMap;
    }


    public Map<String, Object> getRfIdwithHashMap(List<AssetOutTruth> assetOutTruths) {


        HashMap<Integer,Integer> typeNumhashMap = new HashMap<Integer, Integer>();

        HashMap<Integer,List<String>> typeRfidHashMap = new HashMap();

        for(AssetOutTruth assetOutTruth : assetOutTruths){
            String assetInfoRfidCode = assetOutTruth.getAssetInfoRfidCode();
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(assetInfoRfidCode);
            List<AssetInfo> assetInfos = assetInfoMapper.selectByExample(assetInfoExample);
            //TODO 如果没有应该有报错
            if(assetInfos!=null && assetInfos.size()>0){
                AssetInfo assetInfo = assetInfos.get(0);
                assetOutTruth.setStatus(assetInfo.getStatus());
                Integer assetBaseid = assetInfo.getAssetBaseid();
                AssetBase assetBase = assetBaseMapper.selectByPrimaryKey(assetBaseid);
                Integer assetTypeId = assetBase.getAssetTypeId();
                AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
                String name = assetType.getName();
                assetOutTruth.setTypeName(name);
                if(typeNumhashMap.get(assetTypeId)==null){
                    typeNumhashMap.put(assetTypeId ,1 );
                    List<String> list = new ArrayList<String>();
                    list.add(assetInfoRfidCode);
                    typeRfidHashMap.put(assetTypeId,list);
                }else {
                    int count = typeNumhashMap.get(assetTypeId);
                    count++;
                    typeNumhashMap.put(assetTypeId ,count );
                    List<String> rfIdList = typeRfidHashMap.get(assetTypeId);
                    rfIdList.add(assetInfoRfidCode);
                    typeRfidHashMap.put(assetTypeId,rfIdList);
                }
            }
        }

        Map<String ,Object> resultMap = new HashMap();
        resultMap.put("typeNumhashMap",typeNumhashMap);
        resultMap.put("typeRfidHashMap",typeRfidHashMap);
        return resultMap;
    }

    @Override
    public AssetOutDetailVo getOneTruthDetailOut(String assetOutId, boolean washFlag, int fin) {
        List<AssetOutPlan> planList = assetOutPlanService.getAssetOutPlansOut(assetOutId,washFlag);
        AssetOutDetailVo assetOutDetailVo = new AssetOutDetailVo();

//        for(AssetOutPlan assetOutPlan:planList){
//            if(null == assetOutPlan.getTruthCount()){
//                assetOutPlan.setTruthCount(0);
//            }
//        }

        assetOutDetailVo.setAssetOutPlansList(planList);
        return assetOutDetailVo;
    }

    /**
     * 根据 assetOutId 查找对应的  List<AssetOutTruth> 带分页
     *
     * @param page
     * @return
     */
    @Override
    public PageUtils getAssetOutTruthsWithPage(PageUtils page) {
        AssetOutTruthExample  example = (AssetOutTruthExample) page.getQueryParames();
        int count = assetOutTruthMapper.countByExample(example);
        List<AssetOutTruth> list = assetOutTruthMapper.getAssetOutTruthPages((page.getPage()-1)* page.getLimit(), page.getLimit(), example);
        for (AssetOutTruth assetOutTruth:list){
            if(assetOutTruth.getSecondInputTime()!=null){
                String dateToStr = DateUtils.getDateToStr(assetOutTruth.getSecondInputTime());
                assetOutTruth.setSecondInputTimeStr(dateToStr);
            }
            if(assetOutTruth.getInputTime()!=null){
                String dateToStr = DateUtils.getDateToStr(assetOutTruth.getInputTime());
                assetOutTruth.setInputTimeStr(dateToStr);
            }

        }

        getIntegerIntegerHashMap(list);
        page.setList(list);
        page.setCountNum(count);
        return page;
    }

}